<?php

namespace oa\services;

use Yii;
use oa\models\DeliveryRecord;

class DeliveryRecordService extends BaseService
{
    public static function getList()
    {
        $req = Yii::$app->request;
        $query = DeliveryRecord::find();
        $query->select('r.*,concat(u.unit,u.province_name,u.city_name,u.county_name) pcc');
        $query->alias('r');
        $query->where('r.status<>-1');
        $query->filterWhere(['r.status' => $req->get('status')]);
        $query->andFilterWhere(['r.code' => $req->get('code')]);
        $query->andFilterWhere(['r.delivery_company_id' => $req->get('delivery_company_id')]);
        $query->andFilterWhere(static::formatTimeSearch('r.created_at'));
        $query->andFilterWhere(static::formatTimeSearch('r.created_at', 1));
        $query->andFilterWhere(static::formatTimeSearch('r.send_at'));
        $query->andFilterWhere(static::formatTimeSearch('r.send_at', 1));
        $query->andFilterWhere(['u.province' => $req->get('province')]);
        $query->andFilterWhere(['u.city' => $req->get('city')]);
        $query->andFilterWhere(['u.county' => $req->get('county')]);
        $query->andFilterWhere(['u.unit' => $req->get('unit')]);
        $query->leftJoinWith(['user u' => function($q) {
            $q->onCondition(['<>', 'u.status', -1]);
        }], false);
        $countQuery = clone $query;
        $query->orderBy(['created_at' => SORT_DESC]);
        $rows = $query->offset($req->get('offset'))->limit($req->get('limit'))->asArray()->all();
        foreach ($rows as $k => $v) {
            $rows[$k]['send_at'] = date('Y-m-d H:i', $v['send_at']);
            $rows[$k]['created_at'] = date('Y-m-d H:i', $v['created_at']);
        }
        return ['rows' => $rows, 'total' => $countQuery->count()];
    }

    public static function getAuditList()
    {
        $req = Yii::$app->request;
        $query = DeliveryRecord::find();
        $query->select('r.*,concat(u.unit,u.province_name,u.city_name,u.county_name) pcc');
        $query->alias('r');
        $query->where('r.status=1');
        $query->filterWhere(['r.code' => $req->get('code')]);
        $query->andFilterWhere(static::formatTimeSearch('r.created_at'));
        $query->andFilterWhere(static::formatTimeSearch('r.created_at', 1));
        $query->andFilterWhere(static::formatTimeSearch('r.send_at'));
        $query->andFilterWhere(static::formatTimeSearch('r.send_at', 1));
        $query->andFilterWhere(['u.unit' => $req->get('unit')]);
        $query->leftJoinWith(['user u' => function($q) {
            $q->onCondition(['<>', 'u.status', -1]);
        }], false);
        $countQuery = clone $query;
        $query->orderBy(['created_at' => SORT_DESC]);
        $rows = $query->offset($req->get('offset'))->limit($req->get('limit'))->asArray()->all();
        foreach ($rows as $k => $v) {
            $rows[$k]['send_at'] = date('Y-m-d H:i', $v['send_at']);
            $rows[$k]['created_at'] = date('Y-m-d H:i', $v['created_at']);
        }
        return ['rows' => $rows, 'total' => $countQuery->count()];
    }

    public static function one($map, $toArray = 0)
    {
        $query = DeliveryRecord::find()->where('status>0');
        if (preg_match('/^[1-9]+[\d]*$/', $map)) {
            $query->andWhere('id=:id', [':id' => $map]);
        } elseif (is_array($map)) {
            $query->andWhere($map[0], $map[1]);
        } else {
            $query->andWhere($map);
        }
        if ($toArray == 1) {
            $query->asArray();
        }
        return $query->one();
    }

    public static function remove($id)
    {
        $model = self::one(['id=:id AND status<3', [':id' => $id]]);
        $model->scenario = DeliveryRecord::SCENARIO_REMOVE;
        $model->status = -1;
        return $model->_save();
    }

    public static function reject($id, $audit_msg)
    {
        $model = self::one(['id=:id AND status=1', [':id' => $id]]);
        $model->scenario = DeliveryRecord::SCENARIO_REJECT;
        $model->status = 2;
        $model->audit_msg = $audit_msg;
        return $model->_save();
    }

    public static function approve($id)
    {
        $model = self::one(['id=:id AND status=2', [':id' => $id]]);
        $model->scenario = DeliveryRecord::SCENARIO_APPROVE;
        $model->status = 3;
        return $model->_save();
    }

    public static function getAllByCid($cid)
    {
        $rows = DeliveryRecord::find()->where('status>0 AND cid=:cid', [':cid' => $id])->asArray()->all();
        foreach ($variable as $key => $value) {
            # code...
        }
    }

}